#include "myhead.h"
#include "head.h"


//登录选项
void LoginOption()
{
    int choice ;
    do{
    CLEAR_SCREEN();
    MOVE_CURSOR(5,25);
    SET_COLOR(COLOR_YELLOW,0);
    printf("登录选项：");

    MOVE_CURSOR(8,26);
    
    printf("1.用户登录：");

    MOVE_CURSOR(11,26);
    printf("2.管理员登录：");

    MOVE_CURSOR(14,26);
    printf("3.退出：");


    choice = getchoice(1,3,17,26);
    switch(choice)
    {
    case 1:
        User_menu();
        break;
    case 2:
        while (1)
        {
           
            int password ;
            MOVE_CURSOR(17,26);
            CLEAR_LINE();
            SET_COLOR(COLOR_TITLE,0);
            printf("请输入密码：");
            scanf("%d",&password);
            if (password == 0)
            {
                break;
            }
            
            else if (password == 1)
            {
                admin_menu();
                break; 
            }
            else{
            SET_COLOR(COLOR_ERROR,0);
            CLEAR_LINE();
            printf("密码错误");
            RESET_COLOR();
            getchar();
            }
        }     
        break;
    case 3:
        return;     
    }

    }while(1);
}

//获取用户输入
void getinput(char *buffer,int max_len,int row,int col)
{
  
    MOVE_CURSOR(row,col);
   

    int pos = 0;
    while (1)
    {
        int c = getchar();
        if (c == '\n')
        {
            buffer[pos] = '\0';
            break;
        }
        else if (c == 127 || c == 8)
        {
            if (pos > 0)
            {
                pos--;
                printf("\b \b");
            }
        }
        else if (pos < max_len-1 && isprint(c))// // 检查可打印字符和缓冲区空间
        {
            buffer[pos] = c;
            pos++;
            putchar(c);
        }  
    }
   
}

//在指定位置获取用户输入并验证输入是否在有效范围内
int getchoice(int min,int max,int row,int col){
    int choice ;

    while (1)
    {
        MOVE_CURSOR(row,col);
        CLEAR_LINE();
        printf("请输入选择%d-%d: ",min,max);
        if (scanf("%d",&choice) != 1)
        {
            while (getchar() != '\n');
            continue;
        }
        getchar();
        
        if (choice <=max && choice >= min)
        {
            return choice;
        }
        
        MOVE_CURSOR(row+1,col);
        CLEAR_LINE();
        SET_COLOR(COLOR_ERROR,0);
        printf("输入错误");
        RESET_COLOR();
        
    }
    return choice;

}

void Enter()
{
    printf("按Enter继续...");
}